﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EventStandsApp
{
    public class Order
    {
        //fields
        private long orderNo;
        private DateTime orderDate;
        private Client c;
        private int noOfDays;
        private double price;
        private List<RentedItem> items;
        List<OrderLine> lines; private static int lineNo;
        DataHelper dh = new DataHelper();

        //Constructor
        public Order(long no, DateTime date, Client c, int days, double price)
        {
            this.orderNo = no;
            this.orderDate = date;
            this.c = c;
            this.noOfDays = days;
            this.price = price;
            items = new List<RentedItem>();
            lines = new List<OrderLine>();
            lineNo = 1;
        }

        //returns the client's id
        public int getClientId()
        {
            return c.ClientId;
        }

        //returns true if he founds a product id in the list, false otherwise
        public bool findProdId(int prodId)
        {
            for (int i = 0; i < lines.Count; i++)
            {
                if (lines[i].ProductId == prodId)
                {
                    return true;
                }
            }
            return false;
        }

        //adds all the orderlines into the list from the database
        public void addAllOrdersLine()
        {
            lines = dh.getAllOrderLine(orderNo);
        }

        //Getters and Setters
        public double Price
        {
            get { return this.price; }
        }
        public long OrderNo
        {
            get { return this.orderNo; }
        }
        public DateTime OrderDate
        {
            get { return this.orderDate; }
            set { this.orderDate = value; }
        }
        public int NoOfDays
        {
            get { return this.noOfDays; }
        }
        public List<RentedItem> getAllItems()
        {
            return items;
        }

        // sets the client to null, the no of days to zero and makes the list empty
        public void clearOrder()
        {
            this.c = null;
            this.noOfDays = 0;

            items = new List<RentedItem>();
        }
        /*
         * adds an order line into the list*/
        public void addOrderLine(int prodId)
        {
            OrderLine ol = new OrderLine(orderNo, lineNo, orderDate, orderDate.AddDays(noOfDays), prodId, "n");
            lines.Add(ol);
            dh.addOrderLine(orderNo, lineNo, orderDate, orderDate.AddDays(noOfDays), prodId, "n");
            lineNo++;
        }

        //returns a list with all the orderlines
        public List<OrderLine> getAllLines()
        {
            return lines;
        }

        //adds the order into the database
        public void processOrder(int standNo)
        {
            dh.addOrder(orderNo, price, standNo, c.ClientId);
        }

        //adds an item into the list
        public void addItem(RentedItem it)
        {
            items.Add(it);
        }

        /* removes an item from the order*/
        public void removeItem(RentedItem it)
        {
            for (int i = 0; i < items.Count; i++)
            {
                if (items[i].ProductId == it.ProductId)
                {
                    items.RemoveAt(i);
                }
            }
        }
    }
}
